<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use think\Session;

class RecordController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $name = input('name');
        $record = Db::name('record r')
            ->join('cmf_student s','s.id=r.student_id','LEFT')
            ->join('cmf_score sc','sc.id=r.score_id','LEFT')
            ->join('cmf_reason re','re.id=r.reason_id','LEFT')
            ->join('cmf_user u','u.id=r.user_id')
            ->where(function (Query $query) use ($name){
                $query->where('s.name','like',"%$name%");
            })
            ->field('r.*,s.name as student_name,sc.name as score_name,re.name as reason_name,u.user_login as user_name')
            ->order('r.id DESC')
            ->paginate('3',false,['query'=>$this->request->param()]);
        //halt($record);
        //登陆者的id

        //echo $user_name;
        $page = $record->render();
        return $this->fetch('',['page'=>$page,'record'=>$record]);
    }

    /**
     * 显示添加资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function add()
    {
        $user_name = session('name');
        //halt($user_name);
        $school=Db::name('school')->select();
        $reason =Db::name('reason')->select();
        $score=Db::name('score')->select();
        return $this->fetch('record_add',[
            'school'=>$school,
            'reason'=>$reason,
            'score'=>$score,
            'user_name'=>$user_name
        ]);
    }
    public function addPost()
    {
        if(request()->isPost()){
            $data=input();
            $data['user_id']=Session('ADMIN_ID');
            unset($data['photo_name']);
            $data['create_time']=date('Y-m-d H:i:s',time());
            //halt($data);
            $add=Db::name('record')->insert($data);
            if($add)
                $this->success('添加成功','record/index');
                $this->error('添加失败');
        }
    }
    public function getRoom()
    {
        //获取当前school_id
        $schoolId=$this->request->post();
        //查询此学校下的班级
        $room=Db::name('room')->where(['sid'=>$schoolId])->select();
        $options=["<option value=''>请选择班级</option>"];
        //如果有班级就遍历输出
        if ($room) {
            foreach ($room as $v) {
                $opt = '<option value="' . $v['id'] . '">' . $v['name'] . '</option>';
                //向$options中添加$opt的值
                array_push($options, $opt);
            }
            //返回值
            return $options;
        }
    }

    public function getStudent()
    {
        //获取当前school_id
        $roomId=$this->request->post();
        //查询此学校下的班级
        $student=Db::name('student')->where(['cid'=>$roomId])->select();
        $options=["<option value=''>请选择学生</option>"];
        //如果有班级就遍历输出
        if ($student) {
            foreach ($student as $v) {
                $opt = '<option value="' . $v['id'] . '">' . $v['name'] . '</option>';
                //向$options中添加$opt的值
                array_push($options, $opt);
            }
            //返回值
            return $options;
        }
    }




    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $record = Db::name('record')->get($id);
        $userId=$record['user_id'];
        $user = Db::name('user u')
            ->join('cmf_record r','u.id=r.user_id','LEFT')
            ->field('u.user_login as user_name')
            ->where('u.id',$userId)
            ->select()
            ->toArray();
        $user = $user[0];
        //halt($user['user_name']);
        $studentId = $record['student_id'];
        $student = Db::name('student s')
            ->join('cmf_room r','r.id=s.cid')
            ->join('cmf_school sc','sc.id=r.sid')
            ->where('s.id',$studentId)
            ->field('s.id,r.id as room_id,sc.id as school_id')
            ->select();
        $student=$student[0];
        //halt($student);
        $school_id =$student['school_id'];
        $record['school_id']=$school_id;
        $room_id =$student['room_id'];
        $record['room_id']=$room_id;
        $school = Db::name('school')->select();
        $room = Db::name('room')->where('sid',$student['school_id'])->select();
        $student = Db::name('student')->where('cid',$student['room_id'])->select();
        $reason = Db::name('reason')->select();
        $score = Db::name('score')->select();
        return $this->fetch('record_edit',[
            'student'=>$student,
            'reason'=>$reason,
            'score'=>$score,
            'record'=>$record,
            'school'=>$school,
            'room'=>$room,
            'user'=>$user
        ]);
    }
    public function editPost()
    {
        if(request()->isPost()){
            $data = input();
            unset($data['photo_name']);
            $data['user_id']=Session('ADMIN_ID');
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $edit = Db::name('record')->update($data);
            if($edit)
                $this->success('修改成功','record/index');
                $this->error('修改失败');
        }
    }



    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $del=Db::name('record')->delete($id);
        if($del)
            $this->success('删除成功','record/index');
            $this->error('删除失败');
    }
}
